<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/1
 * Time: 18:48
 */

namespace app\admin\controller;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Statistics extends Common
{

    public function statistics()
    {
        //（134-104）/134*100％＝22.39％
        //上升率：（28-12）/12*100％＝133.33％

        //echo '<br>上周起始时间:<br>';
        $last_week_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
        $last_week_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
//        echo '<br>本周起始时间:<br>';
        $start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
        $end_time = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));
        $last_user_num = Db::name('user')->whereTime('registration_time', [$last_week_start, $last_week_end])->count();
        $new_user_num = Db::name('user')->whereTime('registration_time', [$start_time, $end_time])->count();
        if ($last_user_num == 0 || $new_user_num == 0 ){
            $user_rate = '0%';
            $user_rate_type = 'eq';
        }else{
            if ($last_user_num < $new_user_num) {
                $user_rate = number_format(($new_user_num - $last_user_num) / $last_user_num * 100, 2) . '%';
                $user_rate_type = 'up';
            } else if ($last_user_num > $new_user_num) {
                $user_rate = number_format(($last_user_num - $new_user_num) / $last_user_num * 100, 2) . '%';
                $user_rate_type = 'down';
            }else{
                $user_rate = '0%';
                $user_rate_type = 'eq';
            }
        }
        $user_num = Db::name('user')->count();
        $last_supplier_num = Db::name('supplier')->whereTime('registration_time',[$last_week_start , $last_week_end])->whereIn('check_status','1,2')->count();
        $new_supplier_num = Db::name('supplier')->whereTime('registration_time',[$start_time , $end_time])->whereIn('check_status','1,2')->count();
        if($last_supplier_num < $new_supplier_num)
        {
            $supplier_rate = number_format(($new_supplier_num - $last_supplier_num) / $last_supplier_num *100 , 2) . '%';
            $supplier_rate_type = 'up';
        }else if($last_supplier_num > $new_user_num){
            $supplier_rate = number_format(($last_supplier_num - $new_supplier_num) / $last_supplier_num * 100 , 2) . '%';
            $supplier_rate_type = 'down';
        }else{
            $supplier_rate = '0%';
            $supplier_rate_type = 'eq';
        }
        $supplier_num = Db::name('supplier')->where('check_status',2)->count();
        $order_num = Db::name('order')->whereIn('status','1,2,3,4')->count();
        $success_order_num = Db::name('order')->whereIn('status','5,6')->count();
        $zs_pro_count = Db::name('product')->where('status',1)->where('is_shelves',1)->count();
        $dsj_pro_count = Db::name('product')->where('status',1)->where('is_shelves',2)->count();
        $type_info = Db::name('base_data')->field('id,name as label')->where(['parent_id' => 0 ,'status' => 1 , 'type_menu' => 1])->select();
        if($type_info){
            $arr = [];
            foreach ($type_info as $key => $value){
                $res = Db::name('product')->where('status',1)->where('category_id',$value['id'])->count();
                if($res)
                {
                    $value['data'] = $res;
                    $arr[] = $value;
                }
            }
        }else{
            $arr = ['id'=>0,'label'=>0,'data'=>0];
        }
        $info['data'] = [
            'user_rate' => $user_rate,
            'user_rate_type' => $user_rate_type,
            'supplier_rate' => $supplier_rate,
            'new_user_num' => $new_user_num,
            'supplier_rate_type' => $supplier_rate_type,
            'user_num' => $user_num,
            'new_supplier_num' => $new_supplier_num,
            'supplier_num' => $supplier_num,
            'order_num' => $order_num,
            'success_order_num' => $success_order_num,
            'zs_pro_count' => $zs_pro_count,
            'dsj_pro_count' => $dsj_pro_count,
            'type_pro_info' => $arr
        ];
        return json($info);
    }
}